local extension = Package("heroes_ev8")
extension.extensionName = "evolution"
--extension.game_modes_whitelist = {"brokenroyal_mode"}

local EV = require "packages/evolution/EV"
local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["heroes_ev8"] = "新约角色29-32",
  ["ev"] = "新约",
}

local Komeiji = General(extension, "ev__komeiji", "ev", 3, 3, General.Female)

local ev__xiangqi = fk.CreateActiveSkill{
  name = "ev__xiangqi",
  prompt = "#ev__xiangqi-prompt",
  target_num = 1,
  card_num = 0,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, to_select, selected, selected_cards, card, extra_data, player)
    return #selected == 0 and to_select ~= player.id and not Fk:currentRoom():getPlayerById(to_select):isKongcheng()
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local cards = table.filter(target:getCardIds("h"), function(id) return Fk:getCardById(id):getMark("@@shown-inhand") == 0 end)
    if #cards > 0 then
      local id = room:askForCardChosen(player, target, {card_data = {{"$Hand", cards}}}, self.name)
      target:showCards(id)
      local card = Fk:getCardById(id)
      if U.canUseCard(room, player, card, false) then
        room:addPlayerMark(target, "xiangqi_buff")
        U.askForUseVirtualCard(room, player, card.name, nil, self.name)
        room:setPlayerMark(target, "xiangqi_buff", 0)
      end
    end
  end,
}


local xiangqi_trigger = fk.CreateTriggerSkill{
  name = "#ev__xiangqi_trigger",
  mute = true,
  events = {fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player.id == data.from and data.card.skillName == "ev__xiangqi"
    and target:getMark("xiangqi_buff") > 0 and not target:isKongcheng()
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local ids = table.filter(target:getCardIds("h"), function (id)
      return table.every(target:getCardIds("h"), function (cid)
        return Fk:getCardById(cid).number >= Fk:getCardById(id).number
      end)
    end)
    local cards = {}
    if #ids == 1 then
      cards = ids
    else
      cards = room:askForCard(target, 1, 1, false, self.name, false, tostring(Exppattern{ id = ids }), "#ev__xiangqi-discard")
    end
    if #cards > 0 then
      room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonDiscard, self.name, "", true, target.id)
    end
  end,
}

local ev__xinhua = fk.CreateViewAsSkill{
  name = "ev__xinhua",
  pattern = ".",
  prompt = "请选择需要使用或打出牌的牌名",
  interaction = function()
    local all_names = U.getAllCardNames("bt")
    local names = {}
    for _, name in ipairs(all_names) do
      local to_use = Fk:cloneCard(name)
      to_use.skillName = "ev__xinhua"
      if ((Fk.currentResponsePattern == nil and to_use.skill:canUse(Self, to_use) and not Self:prohibitUse(to_use)) or
         (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(to_use))) then
        table.insertIfNeed(names, name)
      end
    end
    if #names == 0 then return end
    return U.CardNameBox {choices = names, all_choices = all_names}
  end,
  view_as = function(self, cards)
    if #cards ~= 0 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = self.name
    return card
  end,
  enabled_at_play = Util.FalseFunc,
  enabled_at_response = function(self, player, response)--FIXME：pattern解析目前有问题
    return player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 and player.phase == Player.NotActive
  end,
  before_use = function(self, player, use)
    local room = player.room
    local current = room.current
    local name = use.card.trueName
    local cards = table.filter(current:getCardIds("h"), function (id)
      return Fk:getCardById(id).trueName == name
    end)
    --if #cards == 0 then return "" end
    local ids, _ = U.askforChooseCardsAndChoice(player, cards, {"OK"}, self.name, "#ev__xinhua", {"Cancel"}, 1, 1, current:getCardIds("h"))
    if #ids > 0 then
      current:showCards(ids)
      if #room:askForDiscard(player, 1, 1, true, self.name, true) > 0 then
      else
        return ""
      end
    else
      return ""
    end
  end,
}

ev__xiangqi:addRelatedSkill(xiangqi_trigger)
Komeiji:addSkill(ev__xiangqi)
Komeiji:addSkill(ev__xinhua)

Fk:loadTranslationTable{
  ["ev__komeiji"] = "古明地觉",
  ["designer:ev__komeiji"] = "夜之礼赞",
  --["illustrator:ev__archetto"] = "",
  ["ev__xiangqi"] = "想起",
  ["#ev__xiangqi_trigger"] = "想起",
  ["#ev__xiangqi-prompt"] = "你可观看一名其他角色的手牌并展示其一张手牌",
  ["#ev__xiangqi-discard"] = "请弃置一张点数最小的手牌",
  [":ev__xiangqi"] = "阶段技，你可观看一名其他角色的手牌并展示其一张手牌，视为使用此牌；当其成为你以此法使用牌的目标后，其弃置一张点数最小的手牌。",
  ["ev__xinhua"] = "心花",
  ["#ev__xinhua"] = "请展示一张你需要使用的牌",
  [":ev__xinhua"] = "回合技，当你于回合外需要使用或打出牌时，你可观看当前回合角色的手牌，且你可展示其手牌中的此牌，弃置一张牌，视为使用或打出之。",
}

local Archetto = General(extension, "ev__archetto", "ev", 3, 3, General.Female)

local baofengzhoushi = fk.CreateActiveSkill{
  name = "baofengzhoushi",
  prompt = "你可摸一张牌并弃置至多三张牌",
  card_num = 0,
  target_num = 0,
  card_filter = Util.FalseFunc,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    player:drawCards(1, self.name)
    local cards = room:askForDiscard(player, 1, 3, true, self.name, true)
    if #cards >= 1 then
      room:setPlayerMark(player, "baofengzhoushi1-phase", 2)
    end
    if #cards >= 2 then
      room:setPlayerMark(player, "baofengzhoushi2-phase", 2)
    end
    if #cards >= 3 then
      room:setPlayerMark(player, "baofengzhoushi3-phase", 2)
    end
  end
}

local baofengzhoushi_trigger = fk.CreateTriggerSkill{
  name = "#baofengzhoushi_trigger",
  mute = true,
  refresh_events = {fk.AfterCardUseDeclared},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Play and data.card.trueName == "slash"
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if player:getMark("baofengzhoushi1-phase") > 0 then
      room:removePlayerMark(player, "baofengzhoushi1-phase")
      player:addCardUseHistory("slash", -1)
    end
    if player:getMark("baofengzhoushi2-phase") > 0 then
      room:removePlayerMark(player, "baofengzhoushi2-phase")
      data.unoffsetableList = table.map(player.room.alive_players, Util.IdMapper)
    end
    if player:getMark("baofengzhoushi3-phase") > 0 then
      room:removePlayerMark(player, "baofengzhoushi3-phase")
    end
  end,
}

local baofengzhoushi_tar = fk.CreateTargetModSkill{
  name = "#baofengzhoushi_targetmod",
  extra_target_func = function(self, player, skill)
    if skill.trueName == "slash_skill" and player:getMark("baofengzhoushi2-phase") > 0 then
      return 1
    end
  end,
  distance_limit_func =  function(self, player, skill, card)
    if player:getMark("baofengzhoushi1-phase") > 0 and card and card.trueName == "slash" then
      return 999
    end
  end,
}

local landengzhanshu = fk.CreateTriggerSkill{
  name = "landengzhanshu",
  anim_type = "offensive",
  events = {fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and data.card and data.card.trueName == "slash" and EV.isFriend(player, target)
    and target:distanceTo(data.to) > 1 and player:usedSkillTimes(self.name) == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = {}
    local wordcard = room:askForCard(player, 1, 1, true, self.name, true, ".", "#landengzhanshu-recast")
    if #wordcard > 0 then
      room:recastCard(wordcard, player, self.name)
      table.insert(cards, wordcard[1])
    end
    local needcard = room:askForCard(target, 1, 1, true, self.name, true, ".", "#landengzhanshu-recast")
    if #needcard > 0 then
      room:recastCard(needcard, target, self.name)
      table.insert(cards, needcard[1])
    end
    local slashs = table.filter(cards, function (id) return Fk:getCardById(id).trueName == "slash" end)
    if #slashs > 0 then
      local targets = {player.id}
      table.insertIfNeed(targets, target.id)
      local tos = room:askForChoosePlayers(player, targets, 1, 1, "#landengzhanshu-give", self.name, false)
      if #tos > 0 then
        room:moveCardTo(slashs, Card.PlayerHand, room:getPlayerById(tos[1]), fk.ReasonGive, self.name, "", true)
      end
    end
  end,
}

baofengzhoushi:addRelatedSkill(baofengzhoushi_trigger)
baofengzhoushi:addRelatedSkill(baofengzhoushi_tar)
Archetto:addSkill(baofengzhoushi)
Archetto:addSkill(landengzhanshu)

Fk:loadTranslationTable{
  ["ev__archetto"] = "空弦",
  ["designer:ev__archetto"] = "言默",
  ["illustrator:ev__archetto"] = "alchemaniaC",
  ["baofengzhoushi"] = "暴风骤矢",
  [":baofengzhoushi"] = "阶段技，你可摸一张牌并弃置至多三张牌，令你于此阶段内使用的下两张【杀】根据弃牌数获得以下效果：不少于1，不计入次数限制且无距离限制；不少于2，不是【闪】的合法目标；不少于3，可额外选择一个目标。",
  ["landengzhanshu"] = "兰登战术",
  ["#landengzhanshu-give"] = "请选择获得【杀】的角色",
  ["#landengzhanshu-recast"] = "兰登战术：你可重铸一张牌",
  [":landengzhanshu"] = "回合技，当己方角色使用【杀】对其距离大于1的角色造成伤害时，你可令你与其各重铸牌，然后将以此法重铸的【杀】交给你或其。",

  ["$baofengzhoushi1"] = "拉弓，瞄准，放手。",
  ["$baofengzhoushi2"] = "感受弓弦的震动。",
  ["$baofengzhoushi3"] = "福音，停留于指尖。",
  ["$baofengzhoushi4"] = "弦有三种选择，而敌人有三种苦难。",
  ["$landengzhanshu"] = "罗德岛将会见识到兰登修道院出色的战术素养。",
  ["~ev__archetto"] = "高墙之外的战斗，原来是这么残酷的吗？",
}

local Qingque = General(extension, "ev__qingque", "ev", 3, 3, General.Female)

local haidilaoyue = fk.CreateTriggerSkill{
  name = "haidilaoyue",
  anim_type = "drawcard",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and player.phase == Player.Play
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x = #table.filter(room:getOtherPlayers(player), function (p) return EV.isFriend(player, p) end)
    local num = x + 1
    while num > 0 do
      if num < x + 1 and not room:askForSkillInvoke(player, self.name, nil, "#haidilaoyue-invoke") then break end
      player:drawCards(2, self.name)
      if player:getHandcardNum() > 4 then
        EV.adjustHandCards(room, player, 4, self.name)
      end
      num = num - 1
    end
    local n = 4 - x + num
    room:setPlayerMark(player, "@haidilaoyue-phase", n)
  end,

  refresh_events = {fk.AfterCardUseDeclared},
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and player.phase == Player.Play and player:getMark("@haidilaoyue-phase") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:removePlayerMark(player, "@haidilaoyue-phase")
    if player:getMark("@haidilaoyue-phase") == 0 then
      player.room:addPlayerMark(player, "haidilaoyue-phase")
    end
  end
}

local haidilaoyue_prohibit = fk.CreateProhibitSkill{
  name = "#haidilaoyue_prohibit",
  prohibit_use = function(self, player, card)
    return player:getMark("haidilaoyue-phase") > 0 and Fk:currentRoom():getCardArea(card) == Card.PlayerHand
  end,
}

local gangshangkaihua = fk.CreateTriggerSkill{
  name = "gangshangkaihua",
  events = {fk.TargetSpecifying},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 then
      return data.card.type ~= Card.TypeEquip
    end 
  end,
  on_cost = function(self, event, target, player, data)
    local cards = player.room:askForCard(player, 3, 3, false, self.name, true, ".", "#gangshangkaihua")
    if #cards > 0 then
      self.cost_data = cards
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:showCards(self.cost_data)
    if table.every(self.cost_data, function (id) return Fk:getCardById(id).suit == data.card.suit end)
    or table.every(self.cost_data, function (id) return Fk:getCardById(id).type == data.card.type end) then
      data.extra_data = data.extra_data or {}
      data.extra_data.gangshangkaihua = data.extra_data.gangshangkaihua or {}
      table.insertIfNeed(data.extra_data.gangshangkaihua, data.to)
    end
    if table.every(self.cost_data, function (id)
      return Fk:getCardById(id).type == data.card.type and Fk:getCardById(id).suit == data.card.suit
    end) then
      local targets = U.getUseExtraTargets(player.room, data, false, true)
      local tos = player.room:askForChoosePlayers(player, targets, 1, 2, "#gangshangkaihua-choose", self.name, true)
      if #tos > 0 then
        for _, pid in ipairs(tos) do
          AimGroup:addTargets(room, data, pid)
        end
        room:sendLog{
          type = "#AddTargetsBySkill",
          from = player.id,
          to = tos,
          arg = self.name,
          arg2 = data.card:toLogString()
        }
      end
    end
  end,

  refresh_events = {fk.DamageCaused},
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(self) and data.card then
      local e = player.room.logic:getCurrentEvent():findParent(GameEvent.CardEffect)
      return e and e.data[1].extra_data and e.data[1].extra_data.gangshangkaihua
      and table.contains(e.data[1].extra_data.gangshangkaihua, data.to.id)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    data.damage = data.damage + 1
  end,
}

haidilaoyue:addRelatedSkill(haidilaoyue_prohibit)
Qingque:addSkill(haidilaoyue)
Qingque:addSkill(gangshangkaihua)

Fk:loadTranslationTable{
  ["ev__qingque"] = "青雀",
  ["designer:ev__qingque"] = "K之卡",
  ["haidilaoyue"] = "海底捞月",
  ["#haidilaoyue-invoke"] = "你可以重复此流程",
  [":haidilaoyue"] = "出牌阶段开始时，你可{摸两张牌，将手牌弃置至四张}，然后重复{ }里的流程X次；若如此做，你于本阶段内至多使用4-X张牌（X至多为其他己方角色数）。",
  ["@haidilaoyue-phase"] = "可使用",
  ["#haidilaoyue_prohibit"] = "海底捞月",
  ["gangshangkaihua"] = "杠上开花",
  [":gangshangkaihua"] = "回合技，当你使用非装备牌指定一个目标时，你可展示三张手牌，若这四张牌：花色均相同或类别均相同，此牌对其造成的伤害+1；花色与类别均相同，此牌可增加至多两个目标。",
  ["#gangshangkaihua"] = "杠上开花：你可展示三张手牌",
  ["#gangshangkaihua-choose"] = "杠上开花：你可增加至多两个目标",

  ["$haidilaoyue1"] = "手握顺风局，闷声发大财~",
  ["$haidilaoyue2"] = "撞上逆风局，少输就是赢~",
  ["$haidilaoyue3"] = "算了也白算，等等牌就来~",
  ["$haidilaoyue4"] = "战局如牌局，见机就行事~",
  ["$haidilaoyue5"] = "牌势无定势，越急越没戏~",
  ["$gangshangkaihua1"] = "好牌不嫌晚！",
  ["$gangshangkaihua2"] = "自摸加杠开！",
  ["~ev__qingque"] = "打牌别打人！",
}

local shenqihuang = General(extension, "ev__shenqihuang", "ev", 4)

local xingluoyunbu = fk.CreateTriggerSkill{
  name = "xingluoyunbu",
  anim_type = "offensive",
  events = {fk.TurnStart, fk.TurnEnd},
  derived_piles = {"starfall"},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnStart then
      return player:hasSkill(self) and not EV.isFriend(player, target)
    else
      return player:hasSkill(self) and #player:getPile("$starfall") > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.TurnStart then
      local cards = player.room:askForCard(player, 1, 1, false, self.name, true, ".", "#xingluoyunbu")
      if #cards > 0 then
        self.cost_data = cards
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnStart then
      --player:addToPile("$starfall", self.cost_data, true, self.name)
      room:moveCardTo(self.cost_data, Card.PlayerSpecial, player, fk.ReasonJustMove, self.name, "$starfall", false)
    else
      local ids = player:getPile("$starfall")
      room:moveCardTo(ids, Card.Processing, nil, fk.ReasonJustMove, self.name)
      player:addToPile("starfall", ids, true, self.name)
      if #room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
        local use = e.data[1]
        return use.from == target.id and use.card.type == Fk:getCardById(ids[1]).type
      end, Player.HistoryTurn) == 0 then
        if not target:isKongcheng() then
          local id = room:askForCardChosen(player, target, "h", self.name)
          if id then
            player:addToPile("starfall", id, true, self.name)
          end
        end
      end
    end
  end,
}

local xingliutingji = fk.CreateTriggerSkill{
  name = "xingliutingji",
  anim_type = "offensive",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Play and #player:getPile("starfall") > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = room:askForCard(player, 1, 999, false, self.name, true, ".|.|.|starfall|.|.", "#xingliutingji", "starfall")
    if #card > 0 then
      room:useVirtualCard("meteor_falling", card, player, room:getOtherPlayers(player), self.name, true)
    end
  end,

  refresh_events = {fk.DamageInflicted},
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(self, true, true) and target ~= player and data.card and data.card.skillName == self.name
  end,
  on_refresh = function(self, event, target, player, data)
    local cards = data.card.subcards
    local colors = {}
    for _, id in ipairs(cards) do
      table.insertIfNeed(colors, Fk:getCardById(id):getColorString())
    end
    player.room:setPlayerMark(target, "xingliutingji-turn", colors)
  end,
}

local xingliutingji_prohibit = fk.CreateProhibitSkill{
  name = "#xingliutingji_prohibit",
  prohibit_use = function(self, player, card)
    if #player:getTableMark("xingliutingji-turn") ~= 0 then
      return table.contains(player:getTableMark("xingliutingji-turn"), card:getColorString())
    end
  end,
  prohibit_response = function(self, player, card)
    if #player:getTableMark("xingliutingji-turn") ~= 0 then
      return table.contains(player:getTableMark("xingliutingji-turn"), card:getColorString())
    end
  end,
}

xingliutingji:addRelatedSkill(xingliutingji_prohibit)
shenqihuang:addSkill(xingluoyunbu)
shenqihuang:addSkill(xingliutingji)

Fk:loadTranslationTable{
  ["ev__shenqihuang"] = "神启荒",
  ["designer:ev__shenqihuang"] = "次氯酸512",
  ["xingluoyunbu"] = "星罗云布",
  ["$starfall"] = "星陨",
  ["starfall"] = "星陨",
  [":xingluoyunbu"] = "回合级（始），若其不为己方角色，你可将一张手牌扣置于角色牌上并于此回合结束时亮出，称为“星”；若其于此回合内未使用过与之类别相同的牌，你将其一张手牌置为“星”。",
  ["#xingluoyunbu"] = "你可将一张手牌扣置于角色牌上",
  ["xingliutingji"] = "星流霆击",
  [":xingliutingji"] = "出牌阶段开始时，你可将任意张“星”当【星辰陨落】使用，其他角色受到此【星辰陨落】造成的伤害时，其于本回合内不能使用或打出与转化底牌颜色相同的牌。",
  ["#xingliutingji"] = "你可将任意张“星”当【星辰陨落】使用",
  ["#xingliutingji_prohibit"] = "星流霆击",
}

local tianzhao = General(extension, "ev__tianzhao", "ev", 4, 4, General.Female)

local ev__tianhui = fk.CreateTriggerSkill{
  name = "ev__tianhui",
  anim_type = "support",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card.is_damage_card and player:usedSkillTimes(self.name, Player.HistoryRound) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local friends = table.filter(player.room:getOtherPlayers(player), function (p) return EV.isFriend(player, p) end)
    if #friends > 0 then
      local targets = table.filter(friends, function (p)
        return table.every(friends, function (sp)
          return sp:getHandcardNum() <= p:getHandcardNum()
        end)
      end)
      if #targets > 0 then
        local tos = player.room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#ev__tianhui:::"..data.card:toLogString(), self.name, true)
        if #tos > 0 then
          self.cost_data = tos[1]
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local to = player.room:getPlayerById(self.cost_data)
    player.room:moveCardTo(data.card, Card.PlayerHand, to, fk.ReasonGive, self.name, "", true)
    player.room:addPlayerMark(to, "@@ev__tianhui-round")
    if to.hp == 1 and player.room:askForSkillInvoke(to, self.name, nil, "#tianhui-shield:::"..data.card:toLogString()) then
      to:addToPile("shield", data.card, true, self.name)
    end
  end,
}

local ev__wumie = fk.CreateTriggerSkill{
  name = "ev__wumie",
  anim_type = "special",
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target ~= player and EV.isFriend(player, target) and data.card.is_damage_card
    and player:usedSkillTimes(self.name, Player.HistoryRound) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local success, dat = player.room:askForUseActiveSkill(player, "ev__wumie_viewas", "#ev__wumie-use:::"..data.card:toLogString(),
    true, {vs_name = data.card.name, card_filter = data.card.color})
    if success then
      self.cost_data = dat
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if target:getMark("@@ev__tianhui-round") > 0 then
      player:setSkillUseHistory(self.name, 0, Player.HistoryRound)
    end
    local card = Fk:cloneCard(data.card.name)
    card.skillName = self.name
    card:addSubcard(self.cost_data.cards[1])
    player.room:useCard{
      from = player.id,
      tos = table.map(self.cost_data.targets, function(id) return {id} end),
      card = card,
      extraUse = true,
    }
  end,
}

local ev__wumie_viewas = fk.CreateViewAsSkill{
  name = "ev__wumie_viewas",
  pattern = ".",
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).color == self.card_filter
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then return end
    local card = Fk:cloneCard(self.vs_name)
    card:addSubcards(cards)
    card.skillName = "ev__wumie"
    return card
  end,
}

Fk:addSkill(ev__wumie_viewas)
tianzhao:addSkill(ev__tianhui)
tianzhao:addSkill(ev__wumie)

Fk:loadTranslationTable{
  ["ev__tianzhao"] = "天照",
  ["designer:ev__tianzhao"] = "次氯酸512",
  ["ev__tianhui"] = "天晖",
  ["@@ev__tianhui-round"] = "天晖",
  ["#ev__tianhui"] = "你可将%arg交给一名手牌最多的己方角色",
  ["#tianhui-shield"] = "你可将%arg置为护盾",
  [":ev__tianhui"] = "轮次技，当你使用可造成伤害的牌时，你可将此牌交给除你外手牌数最多的一名己方角色；若其体力值为1，其可将此牌置为护盾。",
  ["ev__wumie"] = "无灭",
  ["ev__wumie_viewas"] = "无灭",
  ["#ev__wumie-use"] = "你可将一张与%arg颜色相同的牌当之使用",
  [":ev__wumie"] = "轮次技，其他己方角色使用的可造成伤害的牌结算结束后，你可将一张与此牌颜色相同的牌当此牌使用；然后若其于本轮内因“天晖”获得过牌，你重置“无灭”。",
}

local xunxiangxing = General(extension, "ev__xunxiangxing", "ev", 4)

local fumengminxiang = fk.CreateTriggerSkill{
  name = "fumengminxiang",
  anim_type = "control",
  events = {fk.TargetSpecified, fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TargetSpecified then
      return player:hasSkill(self) and target == player
    else
      return player:hasSkill(self) and target == player and #player:getTableMark("@fumengminxiang-turn") > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.TargetSpecified then
      return player.room:askForSkillInvoke(player, self.name, nil, "#fumengminxiang::"..data.to)
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TargetSpecified then
      local suits = player:getTableMark("@fumengminxiang-turn")
      table.insertIfNeed(suits, data.card:getSuitString(true))
      room:setPlayerMark(player, "@fumengminxiang-turn", suits)
      local to = room:getPlayerById(data.to)
      local cards = room:askForDiscard(to, 1, 1, true, self.name, true, ".", "#fumengminxiang-throw")
      if #cards == 0 then
        local mark = to:getTableMark("fumengminxiang-turn")
        table.insertIfNeed(mark, data.card:getSuitString())
        room:setPlayerMark(to, "fumengminxiang-turn", mark)
      end
    else
      for _, p in ipairs(room.alive_players) do
        if #p:getTableMark("fumengminxiang-turn") > 0 and not p:isKongcheng() then
          p:showCards(p:getCardIds("h"))
          if table.every(p:getCardIds("h"), function (id)
            return table.contains(player:getTableMark("@fumengminxiang-turn"), Fk:getCardById(id):getSuitString(true)) end) then
            p:turnOver()
          end
        end
      end
    end
  end,
}

local fumengminxiang_prohibit = fk.CreateProhibitSkill{
  name = "#fumengminxiang_prohibit",
  prohibit_use = function(self, player, card)
    if #player:getTableMark("fumengminxiang-turn") ~= 0 then
      return table.contains(player:getTableMark("fumengminxiang-turn"), card:getSuitString())
    end
  end,
  prohibit_response = function(self, player, card)
    if #player:getTableMark("fumengminxiang-turn") ~= 0 then
      return table.contains(player:getTableMark("fumengminxiang-turn"), card:getSuitString())
    end
  end,
}

fumengminxiang:addRelatedSkill(fumengminxiang_prohibit)
xunxiangxing:addSkill(fumengminxiang)

Fk:loadTranslationTable{
  ["ev__xunxiangxing"] = "寻香行",
  ["designer:ev__xunxiangxing"] = "次氯酸512",
  ["fumengminxiang"] = "缚梦明香",
  ["#fumengminxiang"] = "你可以对%dest发动【缚梦冥香】",
  ["#fumengminxiang_prohibit"] = "缚梦明香",
  ["@fumengminxiang-turn"] = "冥香",
  ["#fumengminxiang-throw"] = "缚梦明香：请弃置一张牌，否则此回合不能使用或打出此花色的牌",
  [":fumengminxiang"] = "当你使用牌指定目标后，你可记录此花色，令目标角色各选择一项：1.弃置一张牌；2.于当前回合内不能使用或打出此花色的牌，且于此回合结束时展示所有手牌；若其手牌的花色均为你于此回合内记录过的花色，其翻至背面。",
}

local huojinshen = General(extension, "ev__huojinshen", "ev", 3, 3, General.Female)

local ev__daoyan = fk.CreateTriggerSkill{
  name = "ev__daoyan",
  derived_piles = {"ev__zhousha"},
  events = {fk.GameStart, fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    if event == fk.GameStart then
      return player:hasSkill(self)
    else
      return player:hasSkill(self) and #player:getPile("ev__zhousha") > 0 and EV.isFriend(player, target)
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.GameStart then
      return true
    else
      local card = {}
      local unshown = table.filter(player:getCardIds(Player.Hand), function(id) return Fk:getCardById(id):getMark("@@shown-inhand") == 0 end)
      if target == player then
        card = player.room:askForCard(player, 1, 1, true, self.name, true, tostring(Exppattern{ id = unshown }), "#ev__daoyan-show")
      else
        card = player.room:askForCard(player, 1, 1, true, self.name, true, ".", "#ev__daoyan-discard::"..target.id)
      end
      if #card > 0 then
        self.cost_data = card
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      local cards = player:drawCards(7, self.name)
      player:addToPile("ev__zhousha", cards, true, self.name)
    else
      if target == player then
        player:showCards(self.cost_data)
      else
        room:moveCardTo(self.cost_data, Card.DiscardPile, nil, fk.ReasonDiscard, self.name, nil, true, player.id)
      end
      if target.dead then return end
      local id = player.room:askForCardChosen(player, player, {
        card_data = {
          {"ev__zhousha", player:getPile("ev__zhousha")},
        }
      }, self.name)
      room:moveCardTo(id, Card.PlayerSpecial, target, fk.ReasonJustMove, self.name, "shield")
    end
  end,
}

local daoyan_prohibit = fk.CreateProhibitSkill{
  name = "#ev__daoyan_prohibit",
  is_prohibited = function(self, from, to, card)
    return to:hasSkill(self) and #to:getPile("ev__zhousha") > 3 and card.type == Card.TypeTrick
  end,
}

local ev__huozhou = fk.CreateTriggerSkill{
  name = "ev__huozhou",
  anim_type = "offensive",
  events = {fk.RoundStart, fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    if event == fk.RoundStart then
      return player:hasSkill(self)
    else
      if player:hasSkill(self) and data.card.trueName == "slash" and EV.isFriend(player, target) then
        local from = player.room:getPlayerById(data.from)
        return from:getMark("@@huozhou-round") > 0 or player:getPile("ev__zhousha") == 0
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.RoundStart then
      local targets = table.map(room.alive_players, Util.IdMapper)
      local tos = room:askForChoosePlayers(player, targets, 1, 1, "#ev__huozhou", self.name, true)
      if #tos > 0 then
        self.cost_data = tos[1]
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.RoundStart then
      room:addPlayerMark(room:getPlayerById(self.cost_data), "@@huozhou-round")
      room:setPlayerMark(player, "huozhou-round", {self.cost_data})
    else
      local from = room:getPlayerById(data.from)
      local victim = room:getPlayerById(player:getTableMark("huozhou-round")[1])
      if victim.dead then return end
      if from:getHandcardNum() > 0 and room:askForSkillInvoke(victim, self.name, nil, "#ev__huozhou-buff:"..data.from) then
        local id = room:askForCardChosen(victim, from, "h", self.name)
        if id then
          room:moveCardTo(id, Card.DiscardPile, nil, fk.ReasonDiscard, self.name, "", true, player.id) 
        end
      else
        room:loseHp(victim, 1, self.name)
      end
    end
  end,
}

ev__daoyan:addRelatedSkill(daoyan_prohibit)
huojinshen:addSkill(ev__daoyan)
huojinshen:addSkill(ev__huozhou)

Fk:loadTranslationTable{
  ["ev__huojinshen"] = "祸津神",
  ["designer:ev__huojinshen"] = "次氯酸512",
  ["ev__daoyan"] = "祷言",
  ["#ev__daoyan_prohibit"] = "祷言",
  ["#ev__daoyan-show"] = "祷言：你可明置一张牌，将一张咒纱置为你的护盾",
  ["#ev__daoyan-discard"] = "祷言：你可弃置一张牌，将一张咒纱置为%dest的护盾",
  ["ev__huozhou"] = "祸咒",
  ["ev__zhousha"] = "咒纱",
  ["@@huozhou-round"] = "祸咒",
  [":ev__daoyan"] = "游戏开始时，你摸七张牌并置于角色牌上，称为“咒纱”，当你/其他己方角色受到伤害后，你可明置/弃置一张牌，将一张“咒纱”置为你/其的护盾。若“咒纱”数量大于3，你不为其他角色使用魔法牌的合法目标。",
  [":ev__huozhou"] = "轮次技（始），你可选择一名角色：当【杀】于此轮指定己方角色为目标后，若使用者为其或你没有“咒纱”，其须弃置使用者的一张手牌，否则其失去1点体力。",
  ["#ev__huozhou"] = "祸咒：你可选择一名角色",
  ["#ev__huozhou-buff"] = "祸咒：请弃置%src一张手牌，否则你失去1点体力",
}

local datiangou = General(extension, "ev__datiangou", "ev", 4)

local gangtiezhiyu = fk.CreateTriggerSkill{
  name = "gangtiezhiyu",
  anim_type = "defensive",
  events = {fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card.type == Card.TypeTrick and not player:isKongcheng()
  end,
  on_cost = function(self, event, target, player, data)
    local unshown = table.filter(player:getCardIds(Player.Hand), function(id) return Fk:getCardById(id):getMark("@@shown-inhand") == 0 and Fk:getCardById(id).color == Card.Black end) -- and Fk:getCardById(id).color == data.card.color
    local cards = player.room:askForCard(player, 1, 1, true, self.name, true, tostring(Exppattern{ id = unshown }), "#gangtiezhiyu-show")
    if #cards > 0 then
      self.cost_data = cards
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    EV.OpenCards(room, player, self.cost_data, self.name)
    local card = Fk:getCardById(self.cost_data[1])
    if card.suit == data.card.suit then
      EV.nullifiedCard(data, data.card, player)
    else
      room:addPlayerMark(player, "@gangtiezhiyu")
    end
  end,

  refresh_events = {fk.CardUsing},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player:getMark("@gangtiezhiyu") > 0 and data.card.trueName == "slash"
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@gangtiezhiyu", 0)
    data.additionalDamage = (data.additionalDamage or 0) + 1
  end,
}

local yurenbaofeng = fk.CreateActiveSkill{
  name = "yurenbaofeng",
  anim_type = "offensive",
  card_num = function (self)
    return Self:usedSkillTimes(self.name, Player.HistoryPhase) + 1
  end,
  target_num = 0,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name) < 4 and not player:isKongcheng()
  end,
  prompt = "#yurenbaofeng",
  card_filter = function(self, to_select, selected)
    return not Self:prohibitDiscard(Fk:getCardById(to_select))
  end,
  target_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:moveCardTo(effect.cards, Card.DiscardPile, nil, fk.ReasonDiscard, self.name, "", true, player.id)
    local targets = {}
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if not p:isKongcheng() then
        local cards = room:askForDiscard(p, 1, 1, false, self.name, false, ".", "#yurenbaofeng-throw")
        if table.find(effect.cards, function (id)
          return Fk:getCardById(cards[1]).suit == Fk:getCardById(id).suit
        end) then
          table.insertIfNeed(targets, p.id)
        end
        if Fk:getCardById(cards[1]).trueName == "jink" then
          room:addPlayerMark(player, "yurenbaofeng-phase")
        end
      end
    end
    if #targets > 0 then
      local tos = room:askForChoosePlayers(player, targets, 1, 99, "#yurenbaofeng-choose", self.name, true)
      if #tos > 0 then
        room:useVirtualCard("ev__slash", nil, player, table.map(tos, Util.Id2PlayerMapper), self.name, true)
      end
    end
  end,
}

datiangou:addSkill(gangtiezhiyu)
datiangou:addSkill(yurenbaofeng)

Fk:loadTranslationTable{
  ["ev__datiangou"] = "大天狗",
  ["designer:ev__huojinshen"] = "次氯酸512",
  ["gangtiezhiyu"] = "钢铁之羽",
  ["@gangtiezhiyu"] = "钢铁之羽",
  [":gangtiezhiyu"] = "当你成为魔法牌的目标时，你可明置一张黑色牌，若花色与之相同/不同，你令之对你无效/你使用下一张【杀】的伤害基数改为2。",
  ["#gangtiezhiyu-show"] = "钢铁之羽：你可明置一张与%arg颜色相同的牌",
  ["yurenbaofeng"] = "羽刃暴风",
  ["#yurenbaofeng"] = "你可令所有其他角色各弃置一张手牌",
  ["#yurenbaofeng-throw"] = "请弃置一张手牌",
  ["#yurenbaofeng-choose"] = "你可选择任意名角色，视为使用目标为这些角色的【杀】",
  [":yurenbaofeng"] = "阶段技（4），你可弃置X张牌，令所有其他角色各弃置一张手牌，然后你视为对任意名与你以此法弃置相同花色的牌的角色使用【杀】。（X为你于此阶段发动此技能的次数+1）。",
}


return extension